// -------------------------------------------------------------------------
// Copyright (c) 2000-2009 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author: lxt
//
// -------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
// -------------------------------------------------------------------------

package com.ufinity.estimation.service.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.dao.DataAccessException;

import com.cn.ufinity.common.log.LoggerFactory;
import com.ufinity.estimation.persistence.Pager;
import com.ufinity.estimation.persistence.ServiceException;
import com.ufinity.estimation.persistence.domain.FixedPrice;
import com.ufinity.estimation.persistence.domain.MaterialPrice;
import com.ufinity.estimation.service.MaterialPriceService;

/**
 * Description of the class
 * 
 * @author lxt
 * @version 1.0
 * @since 2009-12-14
 */

public class MaterialPriceServiceImpl extends
		BaseServiceImpl<MaterialPrice, Long> implements MaterialPriceService {
	public MaterialPriceServiceImpl() {
		super(LoggerFactory.getPersistenceLog(MaterialPriceServiceImpl.class),
				MaterialPrice.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialPrice> findMaterialPriceByTypeId(long Id) {
		String method = "findMaterialPriceByTypeId";
		if (Id == 0) {
			String hql = "from MaterialPrice";
			try {
				List<MaterialPrice> MaterialPriceList = getHibernateTemplate()
						.find(hql);
				return MaterialPriceList;
			} catch (DataAccessException e) {
				String errorMessage = "an error occurs during find MaterialPrices by TypeId: "
						+ Id;
				logger.error(method, errorMessage + e);
				throw new ServiceException(errorMessage, e);
			}

		} else {
			logger.info(method,
					"get MaterialPrices from database by TypeId, the TypeId is:"
							+ Id);
			String hql = "from MaterialPrice as m where m.fixedTypeId = ?";
			try {
				List<MaterialPrice> MaterialPriceList = getHibernateTemplate()
						.find(hql, new Object[] { Id });
				return MaterialPriceList;
			} catch (DataAccessException e) {
				String errorMessage = "an error occurs during find MaterialPrices by TypeId: "
						+ Id;
				logger.error(method, errorMessage + e);
				throw new ServiceException(errorMessage, e);
			}
		}
	}

	@Override
	public void deleteMaterialPriceByTypeId(long Id) {
		String method = "deleteMaterialPriceByTypeId";
		logger.info(method,
				"get MaterialPrices from database by TypeId, the TypeId is:"
						+ Id);
		String hql = "delete from MaterialPrice as m where m.fixedTypeId = ?";
		try {
			getHibernateTemplate().bulkUpdate(hql, Id);
		} catch (DataAccessException e) {
			String errorMessage = "an error occurs during delete MaterialPrices by TypeId: "
					+ Id;
			logger.error(method, errorMessage + e);
			throw new ServiceException(errorMessage, e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<MaterialPrice> findPagerByTypeId(int currentPage, long id) {
		String method = "findPagerById";
		String hql = null;

		try {
			hql = "from MaterialPrice as f where f.fixedTypeId = ?";
			List<MaterialPrice> list = getHibernateTemplate().find(hql,
					new Object[] { id });

			int totalRecords = 0;
			if (!list.isEmpty()) {
				totalRecords = list.size();
			}

			if (pager == null) {
				pager = new Pager<MaterialPrice>(totalRecords);
			} else {
				pager.setTotalRecords(totalRecords);
			}

			if (currentPage > 0) {
				pager.setCurrentPage(currentPage);
			}

			hql = "from MaterialPrice as f where f.fixedTypeId =:fixedTypeId";
			Query query = getSession().createQuery(hql);
			query.setLong("fixedTypeId", id);
			List<MaterialPrice> entityList = (List<MaterialPrice>) query
					.setFirstResult(
							(pager.getCurrentPage()) * pager.getPageSize())
					.setMaxResults(pager.getPageSize()).list();

			if (pager.getEntityList() != null) {
				pager.getEntityList().clear();
			}

			pager.setEntityList(entityList);
			return pager;
		} catch (DataAccessException e) {
			String errorMessage = "an error occurs during get Pager : " + clazz;
			logger.error(method, errorMessage, e);
			throw new ServiceException(errorMessage, e);
		}
	}
}